<?php
require_once ('Controller/Mobile/Common.inc.php');
require_once ('View/Result.inc.php');
require_once ('Model/HouseKeepingPresetMessageModel.inc.php');
require_once ('Model/HouseKeepingPresetMessageCatModel.inc.php');
require_once ('Model/HouseKeepingPresetMessageLanguageModel.inc.php');

final class Controller extends common {
	private $Result;
	private $Loader;
	private $Callback = NULL;
	public function __construct () {
		parent::__construct();
		$trace = debug_backtrace();
		if (isset ($trace[1])) {
			$this->Loader = $trace[1]['object'];
		}
	}

	public function execude () {
		header ('Content-Type: text/xml');
		$this->Result = new Result ();
		$this->HouseKeepingPresetMessageLanguageModel = new HouseKeepingPresetMessageLanguageModel ($this->mysql);
		$this->HouseKeepingPresetMessageCatModel = new HouseKeepingPresetMessageCatModel ($this->mysql);
		$this->HouseKeepingPresetMessageModel = new HouseKeepingPresetMessageModel ($this->mysql);
		$this->defaultPage ();
		print $this->Result->Create ($this->Callback);
	}

	private $HouseKeepingPresetMessageModel;
	private $HouseKeepingPresetMessageCatModel;
	private $HouseKeepingPresetMessageLanguageModel;
//	private $langCode;
	public function defaultPage () {
		$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
		$this->Result->setStatus (200);
		$this->Result->setErrorCode (0);
		$this->Callback = array ($this, 'getHouseKeepingPresetMessageCallBack');
	}

	public function getHouseKeepingPresetMessageCallBack ($dom, $result) {
		$lang = 'en-US';
		if (array_key_exists ('lang', $_REQUEST)) {
			switch ($_REQUEST['lang']) {
				case 'en-US':
				case 'zh-HK':
				case 'zh-CN':
					$lang = $_REQUEST['lang']; 
					break;
			}
		}
		$HouseKeepingPresetMessageCatItem = $this->HouseKeepingPresetMessageCatModel->GetHouseKeepingPresetMessageCat ();
		foreach ($HouseKeepingPresetMessageCatItem as $catid) {
			$catinfo1 = $this->HouseKeepingPresetMessageModel->GetHouseKeepingCatByID ($catid);
			$catinfo2 = $this->HouseKeepingPresetMessageLanguageModel->GetHouseKeepingLanguageByMessageID ($catinfo1[$this->HouseKeepingPresetMessageModel->idRowName], $lang);
			$item = $dom->createElement ('item');
				$item->setAttribute ('IsNeedDateTime', $catinfo1[$this->HouseKeepingPresetMessageModel->IsNeedDateTimeRowName]);
				$item->setAttribute ('IsNeedAdults', $catinfo1[$this->HouseKeepingPresetMessageModel->IsNeedAdultsRowName]);
				$item->setAttribute ('IsNeedKids', $catinfo1[$this->HouseKeepingPresetMessageModel->IsNeedKidsRowName]);
				$item->setAttribute ('IsNeedSeniors', $catinfo1[$this->HouseKeepingPresetMessageModel->IsNeedSeniorsRowName]);
//var_dump($catinfo1);
//print $this->HouseKeepingPresetMessageModel->IsNeedSeniorsRowName;
//				$item->setAttribute ('IsNeedPeople', $catinfo1[$this->HouseKeepingPresetMessageModel->IsNeedPeopleRowName]);
				$item->setAttribute ('IsNeedRemindingCall', $catinfo1[$this->HouseKeepingPresetMessageModel->IsNeedRemindingCall]);
			$item->setAttribute ('max', $catinfo1[$this->HouseKeepingPresetMessageModel->maxRowName]);
			$item->setAttribute ('text', $catinfo2[$this->HouseKeepingPresetMessageLanguageModel->textRowName]);
			$subinfos = $this->HouseKeepingPresetMessageModel->GetHouseKeepingByCatID ($catid);
			foreach ($subinfos as $subinfo1) {
				$item1 = $dom->createElement ('item');
				$subinfo2 = $this->HouseKeepingPresetMessageLanguageModel->GetHouseKeepingLanguageByMessageID ($subinfo1[$this->HouseKeepingPresetMessageModel->idRowName], $lang);
//				print_r ($subinfo1);
//				print_r ($subinfo2);

				$item1->setAttribute ('IsNeedDateTime', $subinfo1[$this->HouseKeepingPresetMessageModel->IsNeedDateTimeRowName]);
				$item1->setAttribute ('IsNeedAdults', $subinfo1[$this->HouseKeepingPresetMessageModel->IsNeedAdultsRowName]);
				$item1->setAttribute ('IsNeedKids', $subinfo1[$this->HouseKeepingPresetMessageModel->IsNeedKidsRowName]);
				$item1->setAttribute ('IsNeedSeniors', $subinfo1[$this->HouseKeepingPresetMessageModel->IsNeedSeniorsRowName]);
//				$item1->setAttribute ('IsNeedPeople', $subinfo1[$this->HouseKeepingPresetMessageModel->IsNeedPeopleRowName]);
				$item1->setAttribute ('IsNeedRemindingCall', $subinfo1[$this->HouseKeepingPresetMessageModel->IsNeedRemindingCall]);
				$item1->setAttribute ('max', $subinfo1[$this->HouseKeepingPresetMessageModel->maxRowName]);
				$item1->setAttribute ('text', $subinfo2[$this->HouseKeepingPresetMessageLanguageModel->textRowName]);
				$item1->setAttribute ('langCode', $subinfo2[$this->HouseKeepingPresetMessageLanguageModel->langCodeRowName]);
				$item->appendChild ($item1);
			}
			$result->appendChild ($item);
		}
	}
}
?>
